package processingdata.meteor;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;

import processing.core.PVector;
import Control.Meteor;

public class MeteorSerializer {
	
	public static void main(String args[])throws IOException
	{
		ArrayList<ArrayList<ArrayList<Meteor>>> meteors=new ArrayList<ArrayList<ArrayList<Meteor>>>();
		BufferedReader inputReader=new BufferedReader(new FileReader(new File("data/meteor.data")));
		HashMap<String,HashMap> monthYearMap=new HashMap<String,HashMap>();
		
		while(inputReader.ready())
		{
			String inputLine=inputReader.readLine();
			String inputLineParts[]=inputLine.split("\t");
			String meteorId=inputLineParts[0];
			String date=inputLineParts[1];
			String time=inputLineParts[2];
			String meteorName=inputLineParts[3];
			String place=inputLineParts[4];
			float latitude=Float.parseFloat(inputLineParts[5]);
			float longitude=Float.parseFloat(inputLineParts[6]);
			Meteor meteor=new Meteor();
			meteor.setMeteorId(meteorId);
			meteor.setMeteorName(meteorName);
			int year=Integer.parseInt(date.split("-")[0]);
			int month=Integer.parseInt(date.split("-")[1]);
			int dayOfMonth=Integer.parseInt(date.split("-")[2]);
			
			int hourOfDay;
			int minute;
			if(time.contains(":")&& time.contains(" "))
			{
				hourOfDay=Integer.parseInt(time.split(" ")[0].split(":")[0]);
				minute=Integer.parseInt(time.split(" ")[0].split(":")[1]);	
			}
			else
			{
				hourOfDay=0;
				minute=0;
			}
			
			
			meteor.setPlace(place);
			GregorianCalendar gregorianCalendarDate=new GregorianCalendar(year, month-1, dayOfMonth, hourOfDay, minute);
			meteor.setDateOfSight(gregorianCalendarDate);
			PVector coordinates=new PVector(latitude,longitude);
			meteor.setCoordinates(coordinates);
			if(monthYearMap.containsKey(month+"/"+year))
			{
				HashMap<Integer,ArrayList> hourMap=monthYearMap.get(month+"/"+year);
				if(hourMap.containsKey(hourOfDay))
				{
					ArrayList<Meteor> tempMeteors=hourMap.get(hourOfDay);
					tempMeteors.add(meteor);
					hourMap.put(hourOfDay, tempMeteors);
				}
				else
				{
					ArrayList<Meteor> tempMeteors=new ArrayList<Meteor>();
					tempMeteors.add(meteor);
					hourMap.put(hourOfDay, tempMeteors);
				}
				monthYearMap.put(month+"/"+year, hourMap);
			}
			else
			{
				HashMap<Integer,ArrayList> hourMap=new HashMap<Integer,ArrayList>();
				ArrayList<Meteor> tempMeteors=new ArrayList<Meteor>();
				tempMeteors.add(meteor);
				hourMap.put(hourOfDay, tempMeteors);
				monthYearMap.put(month+"/"+year, hourMap);
				
			}
			meteors=convertToList(monthYearMap);
			
			//meteors.add(meteor);
		}
		
		ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(new File("data/meteors.ser")));
		oos.writeObject(meteors);
		oos.close();
	}
	
	static ArrayList<ArrayList<ArrayList<Meteor>>> convertToList(HashMap<String,HashMap> monthYearMap)
	{
		ArrayList<ArrayList<ArrayList<Meteor>>> finalMeteors=new ArrayList<ArrayList<ArrayList<Meteor>>>();
		int years[]={2001,2002,2003,2004,2005,2006,2007,2008,2009,2010};
		int months[]={1,2,3,4,5,6,7,8,9,10,11,12};
		int hourOfDays[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23};
		
		for(int y=0;y<years.length;y++)
		{
			int year=years[y];
			for(int m=0;m<months.length;m++)
			{
				
				int month=months[m];
				String monthYearKey=month+"/"+year;
				if(monthYearMap.containsKey(monthYearKey))
				{
					ArrayList<ArrayList<Meteor>> hoursList=new ArrayList<ArrayList<Meteor>>();
					HashMap<String,ArrayList> hourMap=monthYearMap.get(monthYearKey);
					for(int h=0;h<hourOfDays.length;h++)
					{
						
						int hourOfDay=hourOfDays[h];
						if(hourMap.containsKey(hourOfDay))
						{
							
							ArrayList<Meteor> tempMeteors=hourMap.get(hourOfDay);
							hoursList.add(tempMeteors);
							System.out.println(tempMeteors.size());
							
							
						}
						else
						{
							ArrayList<Meteor> tempMeteors=new ArrayList<Meteor>();
							hoursList.add(tempMeteors);
						}
					}
					finalMeteors.add(hoursList);
				}
				else
				{
					ArrayList<ArrayList<Meteor>> tempHourList=new ArrayList<ArrayList<Meteor>>();
					for(int i=0;i<hourOfDays.length;i++)
					{
						ArrayList<Meteor> tempMeteor=new ArrayList<Meteor>();
						tempHourList.add(tempMeteor);
					}
					finalMeteors.add(tempHourList);
				}
			}
		}
		
		return finalMeteors;
	}

}
